package 数组中两个字符串的最小距离;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n;
        n = Integer.parseInt(in.readLine());
        String[] s = in.readLine().split(" ");

        int pos1 = -1, pos2 = -1, res = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            String tmp = in.readLine();
            if (tmp.equals(s[0])) {
                pos1 = i;
                if (pos2 != -1) {
                    res = Math.min(res, pos1 - pos2);
                }
            }
            if (tmp.equals(s[1])) {
                pos2 = i;
                if (pos1 != -1) {
                    res = Math.min(res, pos2 - pos1);
                }
            }
        }
        System.out.println(res == Integer.MAX_VALUE ? -1 : res);
    }
}
